package com.tql.mall.order.controller;

import com.tql.mall.common.util.AuthContextHolder;
import com.tql.mall.model.enums.ProcessStatus;
import com.tql.mall.model.order.OrderInfo;
import com.tql.mall.order.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Author: tyk
 * @Date: 2023/5/26 15:44
 * @Description:
 */
@RestController
@RequestMapping("api/order/inner")
@Api(description = "service-order对内暴露接口")
public class OrderApiController {


    @Autowired
    private OrderService orderService;

    /**
     * 订单页面需要数据
     */
    @GetMapping("/trade")
    @ApiOperation("生成订单页面所需数据")
    public Map<String, Object> trade(HttpServletRequest req) {
        return orderService.trade(AuthContextHolder.getUserId(req));
    }

    /**
     * 根据订单ID查询订单信息
     */
    @GetMapping("getOrderInfo/{orderId}")
    @ApiOperation("根据订单ID查询订单信息")
    public OrderInfo getOrderInfo(@PathVariable Long orderId) {
        return orderService.getOrderInfo(orderId);
    }

    /**
     * 商家查询订单
     */
    @GetMapping("updateOrderStatus/{orderId}/{flag}")
    @ApiOperation("商家查询订单")
    public void updateOrderStatus(@PathVariable Long orderId, @PathVariable Integer flag) {
        // 已发货
        if (flag == 1)
            orderService.updateOrderStatus(orderId, ProcessStatus.DELIVERED);
            // 已签收
        else if (flag == 2)
            orderService.updateOrderStatus(orderId, ProcessStatus.FINISHED);
            // 待退货
        else if (flag == 3)
            orderService.updateOrderStatus(orderId, ProcessStatus.WAIT_DELIVER);
            // 已退货
        else if (flag == 4)
            orderService.updateOrderStatus(orderId, ProcessStatus.RETURNED);
            // 退货失败
        else if (flag == 5)
            orderService.updateOrderStatus(orderId, ProcessStatus.RETURN_FAIL);
    }


}
